/*
Title:
	My First Application
Module:
    Loop & Setup    
Date:	
	3rd July 2013
Hardware:
	Arduino UNO R3 - AtMega328P @ 16Mhz
IDE:
	Atmel Studio 6.1
Description:
	A test to see if everything works.
Expected Output:  
	1) LED on pin 13 should flash at 1 second intervals
	2) The attached DF Robot LCD Keypad Shield should start, and display 
	   the ID of the button being pressed.
History:
	03/07/13 Created	   
Notes:
	"LCDKeypad.cpp" & "LCDKeypad.h" files were added to Arduino Library.
	These files came with the demo code supplied with the LCD Keypad website.
*/
 
// Core Arduino Libraries
#include "Arduino.h"
// DF Robot LCD Keypad Library - Extension of Arduino Liquid Crystal Library
#include "LCDKeypad.h"

// Define the main Arduino methods for use
void setup();
void loop();

// Global Variables
int led = 13; // Pin 13 has an LED connected on most Arduino boards.
bool led_on = true; // Boolean to track state of flashing LED
int localKey = 0; // Variable to hold the ID of a pressed key
int count = 0; // Delay for flashing the LED

// Create the LCD Keypad Object
LCDKeypad lcd;

// ---------------------------------------------------------------------------
// The setup routine runs once when you press reset:
// ---------------------------------------------------------------------------
void setup() 
{
  // Initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
  
  // Initialize the LCD Display
  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Key Grab v0.2");
}

// ---------------------------------------------------------------------------
// The loop routine runs over and over again forever:
// ---------------------------------------------------------------------------
void loop() 
{
	count++;
	
	if (count >= 20) // This creates a 50ms * 20 delay = 1 second for flashing LED
	{
		count = 0; // Reset delay count
		digitalWrite(led, led_on); // Flash LED
		led_on = !led_on; // Toggle LED state for next time
	} // else not time yet
	
	delay(50); // Wait for 50ms

	localKey = lcd.button();
	if (localKey != KEYPAD_NONE)
	{
		lcd.clear();
		lcd.setCursor(0, 0);
		lcd.print("keypad testing:");
		lcd.setCursor(0, 1);
		lcd.print(localKey);
	} // else nothing has been pressed
}

// ---------------------------------------------------------------------------
